package com.yy.wga.admin.service.controller;


import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.date.DateUtils;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.admin.request.AdminBusinessCourseVideoPageRequest;
import com.yy.wga.mp.module.admin.request.AdminBusinessCourseVideoSaveRequest;
import com.yy.wga.mp.module.admin.response.AdminBusinessCourseVideoPageResponse;
import com.yy.wga.mp.module.business.entity.pojo.BusinessCourseVideoDO;
import com.yy.wga.mp.module.business.service.BusinessCourseVideoService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;

/**
 * 运营后台/商学院课程视频配置
 *
 * @author guochao
 * @since 2024-10-31 22:12:53
 */
@RestController
@RequestMapping("admin/business/courseVideo")
public class AdminBusinessCourseVideoController {
    /**
     * 服务对象
     */
    @Resource
    private BusinessCourseVideoService businessCourseVideoService;

    @PostMapping("page")
    public Result<PageBean<AdminBusinessCourseVideoPageResponse>> page(@Valid @RequestBody AdminBusinessCourseVideoPageRequest request) {
        PageBean<AdminBusinessCourseVideoPageResponse> pageBean = businessCourseVideoService.pageData(request);
        return Result.success(pageBean);
    }

    @PostMapping("save")
    public Result<Boolean> save(@Valid @RequestBody AdminBusinessCourseVideoSaveRequest request) {
        if (request.getStartTime() == null) {
            request.setStartTime(new Date());
        }
        if (request.getEndTime() == null) {
            request.setEndTime(DateUtils.dateAddYears(request.getStartTime(), 100));
        }
        BusinessCourseVideoDO config = BeansUtils.map(request, BusinessCourseVideoDO.class);
        config.setAppKey(RequestUserHolder.getBaseRequest().getAppKey());
        businessCourseVideoService.saveOrUpdate(config);
        return Result.success(Boolean.TRUE);
    }

    @GetMapping("del")
    public Result<Boolean> del(@RequestParam("id") Long id) {
        businessCourseVideoService.removeById(id);
        return Result.success(Boolean.TRUE);
    }
}
